<?
class CMain
{
	private $BUFFER;
	
	private $HEAD_TITLE;
	private $HEAD_CSS_FILES;
	private $HEAD_JS_FILES;
	private $HEAD_STRINGS;
	
	
	const SITE_DIRECTORY = 'soulstream';
	const AJAX_CALL_VARIABLE = 'ajax';
	
	static $ERROR = array();
	static $SITE_TEMPLATE_PATH = '/soulstream/templates/main/';//alfa
	
	function __construct()
	{
		define('SITE_ID', 's1');
		define('LANGUAGE_ID', 'ru');
		define('SITE_TEMPLATE_PATH', '/soulstream/templates/main/');

		$this->InitAjaxCall();
		
		$this->BUFFER = new CBuffer();
	}
	
	static function SetError($text = NULL, $sType = NULL)
	{
		if($sType && $text){ self::$ERROR[$sType][] = $text; return true; }
		else if($text){ self::$ERROR[] = $text; return true; }
		else return false;
	}
	
	static function IssetError($sType = NULL)
	{
		if($sType && count(self::$ERROR[$sType])) return true;
		else if(count(self::$ERROR)) return true;
		else return false;
	}
	
	static function GetError($sType = NULL)
	{
		if($sType && count(self::$ERROR[$sType])) return self::$ERROR[$sType];
		else if(count(self::$ERROR)) return self::$ERROR;
		else return false;
	}
	
//	static function ShowError($sType = NULL)
//	{
//		if($sType && count(self::$ERROR[$sType])) ShowStringError(implode('<br />', self::$ERROR[$sType]));
//		if(count(self::$ERROR)) ShowStringError(implode('<br />', self::$ERROR));
//		else return false;
//	}
	
	static function ShowStringError($sError = NULL)
	{
		if($sError) echo '<div style="color:black;border:2px solid red;padding:5px;margin:5px;background:#ffffff">'.$sError.'</div>';
		else return false;
	}
	
	static function ShowArrayError($arError = NULL)
	{
		if(count($arError)) self::ShowStringError(implode('<br />', $arError));
		else return false;
	}
	
	static function InitAjaxCall()
	{
		if($_REQUEST[AJAX_CALL_VARIABLE] == 'Y')
		{
			define('AJAX', true);
			
			if(strlen($_REQUEST[AJAX_ID_VARIABLE]))
			{
				define('AJAX_ID', $_REQUEST[AJAX_ID_VARIABLE]);
			}
		}
		else
		{
			define('AJAX', false);
		}
	}
	
	function SetTitle($sTitle = NULL)
	{
		$this->HEAD_TITLE = $sTitle;
	}
	
	function GetTitle()
	{
		return $this->HEAD_TITLE;
	}
	
	
	function AddToHead($arParams = NULL)
	{
		if(is_array($arParams))
		{
			switch ($arParams['TYPE']) {
				case 'CSS':
					$this->HEAD_CSS_FILES[] = $arParams;
					break;
				case 'JS':
					$this->HEAD_JS_FILES[] = $arParams;
					break;
				case 'STRING':
					$this->HEAD_STRINGS[] = $arParams;
					break;
			
				default:
					break;
			}
		}
		return false;
	}
	
	function GetStringCSS()
	{
		if(is_array($this->HEAD_CSS_FILES) && count($this->HEAD_CSS_FILES))
		{
			foreach ($this->HEAD_CSS_FILES as $arFile)
			{
				$content .= '<link rel="stylesheet" type="text/css" href="'.$arFile['SRC'].'" />'."\n";
			}
			return $content;
		}
		return false;
	}
	
	function GetStringJS()
	{
		if(is_array($this->HEAD_JS_FILES) && count($this->HEAD_JS_FILES))
		{
			foreach ($this->HEAD_JS_FILES as $arFile)
			{
				$content .= '<script type="text/javascript" src="'.$arFile['SRC'].'"></script>'."\n";
			}
			return $content;
		}
		return false;
	}
	

	
	function BuildSite()
	{
		global $APPLICATION;
		
		// stop buffer
		$content = $APPLICATION->BUFFER->GetBuffer();
		
		$pattern = array(
			'/<!--TEMPLATE-SHOW-TITLE-->/i',
			'/<!--TEMPLATE-SHOW-CSS-->/i',
			'/<!--TEMPLATE-SHOW-JS-->/i',
		);
		$replacement = array(
			$APPLICATION->GetTitle(),
			$APPLICATION->GetStringCSS(),
			$APPLICATION->GetStringJS(),
		);

		$content = preg_replace($pattern, $replacement, $content);
		
		echo $content;
	}
	
	function RestartBuffer()
	{
		$this->BUFFER->CleanBuffer();
	}
	
	function GetMessage($name, $aReplace = NULL)
	{
		global $MESS;
		$s = $MESS[$name];
		if($aReplace!==false && is_array($aReplace))
			foreach($aReplace as $search=>$replace)
				$s = str_replace($search, $replace, $s);
		return $s;
	}
	
	function GetCurrentPage()
	{
		return $_SERVER['PHP_SELF'];
	}
}